<template>
  <div class="m-contain">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-user"></i>基本信息
        </el-breadcrumb-item>
        <el-breadcrumb-item>各科成绩</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box" v-if="!this.$common.status.student">
        <el-form :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" v-show="!this.status.isEdit"
                 label-width="100px" class="searchBox">
          <el-form-item label="学号" prop="id" class="inputForm">
            <el-input v-model="ruleForm.id"></el-input>
          </el-form-item>
          <el-form-item class="searchButton">
            <el-button type="primary" icon="el-icon-search" @click="submitForm('ruleForm')"
                       circle></el-button>
            <el-button icon="el-icon-refresh-right" @click="resetForm('ruleForm')" circle></el-button>
            <el-button icon="el-icon-plus" @click="addGrades"
                       circle></el-button>
          </el-form-item>
        </el-form>
      </div>
      <div v-show="status.hasFoundGrades">
        <el-card class="singleInfo-card">
          <el-table
              :data="gradesList.grades"
              height="600"
              border
              style="width: 100%">
            <el-table-column
                prop="gradesKey"
                label="科目">
            </el-table-column>
            <el-table-column
                prop="gradesValue"
                label="成绩">
            </el-table-column>
            <el-table-column label="操作" width="180" align="center">
              <template slot-scope="scope">
                <el-button
                    type="text"
                    icon="el-icon-edit"
                    @click="handleEdit(scope.$index, scope.row)"
                >编辑
                </el-button>
                <el-button
                    type="text"
                    icon="el-icon-delete"
                    class="red"
                    @click="handleDelete(scope.$index, scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </div>
    </div>
    <el-dialog title="编辑" :visible.sync="editVisible"  width="25%" :close-on-click-modal="false">
      <el-form ref="form" :rules="formRules" :model="form" label-width="70px">
        <el-form-item label="学号" class="inputForm" v-show="this.status.isAdd&&!this.status.hasFoundGrades">
          <el-input v-model="form.graduatesId" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="科目" class="inputForm">
          <el-input v-model="form.gradesKey" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="成绩" class="inputForm" prop="gradesValue">
          <el-input v-model.number="form.gradesValue" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import bus from "../components/bus";

export default {
  name: "Grades",
  data() {
    return {
      ruleForm: {
        id: ''
      },
      rules: {
        id: [
          {required: true, message: '请输入学号', trigger: 'blur'},
          {min: 10, max: 10, message: '长度为10 个字符', trigger: 'change'}
        ]
      },
      status: {
        hasFoundGrades: false,
        isAdd: false,
        isEdit: false
      },
      editVisible: false,
      gradesList: {
        graduatesId: null,
        grades: [
          {
            id: null,
            gradesKey: null,
            gradesValue: null
          }
        ]
      },
      form: {
        graduatesId: null,
        id: null,
        gradesKey: null,
        gradesValue: null
      },
      formRules: {
        gradesValue: [
          {required: false, message: '请输入成绩', type: 'number', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    if (this.$store.state.userInfo.roles === 'student') {

      this.$set(this.$common.status, 'student', true)
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          // alert('submit!');
          this.$axios.get('/grades/' + this.ruleForm.id, {
            headers: {
              "Authorization": localStorage.getItem("token")
            }
          }).then(res => {
            _this.$set(_this.gradesList, 'graduatesId', res.data.data.graduatesId)
            _this.$set(_this.gradesList, 'grades', res.data.data.grades)
            this.$set(this.status, "hasFoundGrades", true)
          }).catch(err => {
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$set(this.ruleForm, "id", "");
      this.$set(this.gradesList, "graduatesId", null);
      this.$set(this.gradesList, "grades", null);
      this.$set(this.status, "hasFoundGrades", false)
    },
    addGrades() {
      this.editVisible = true;
      this.$common.resetObject(this.form)
      this.$set(this.status, 'isAdd', true)
      this.$set(this.form, 'graduatesId', this.gradesList.graduatesId)
    },
    handleEdit(index, row) {
      this.$common.objectValueAtoB(row, this.form)
      this.editVisible = true;
      // this.$set(this.status, 'isEdit', true)
    },
    handleDelete(index, row) {
      const _this = this
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _this.gradesList.grades.splice(index, 1)
        _this.$axios.delete('/grades/' + row.id, {
          headers: {
            "Authorization": localStorage.getItem("token")
          }
        }).then(res => {
          console.log(res)
        }).catch(err => {
          console.log(err)
        })
        _this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        });
      });
    },
    saveEdit() {
      const _this = this
      if (this.status.isAdd) {
        this.$axios.post('/grades/save', this.form, {
          headers: {
            "Authorization": localStorage.getItem("token")
          }
        }).then(res => {
          this.$message.success("添加成功")
          this.editVisible = false;
          this.$set(_this.form, 'id', res.data.data.id)
          delete _this.form.graduatesId
          this.gradesList.grades.push(_this.form)
          this.$set(this.status, 'isAdd', false)
        }).catch(err => {
          console.log(err)
        })
      } else {
        for (let index in this.gradesList.grades) {
          if (this.form.id === this.gradesList.grades[index].id) {
            if (this.form.gradesValue === this.gradesList.grades[index].gradesValue) {
              this.editVisible = false;
              this.$message.info("未修改")
            } else {
              this.$axios.post('/grades/update', this.form, {
                headers: {
                  "Authorization": localStorage.getItem("token")
                }
              }).then(res => {
                this.$message.success("修改成功")
                this.$set(this.gradesList.grades[index], 'gradesValue', this.form.gradesValue)
                this.editVisible = false;
              }).catch(err => {
                console.log(err)
              })
            }
            break;
          }
        }
      }
    },
    closeDialog() {
      this.editVisible = false;
      this.$common.resetObject(this.form)
      this.$set(this.status, 'isAdd', false)
      this.$message.info("取消操作")
    }
  }
}
</script>

<style scoped>

</style>